package dataStructures;

import java.io.File;
import java.io.IOException;
import java.sql.Driver;
import java.util.Vector;

import org.apache.commons.io.FileUtils;
import org.apache.hadoop.fs.Path;

public class FilesAndPaths {
	
	private static Vector<File> CODEBOOK_USER_FILE = new Vector<File>();
	private static Vector<File> CODEBOOK_ITEM_FILE = new Vector<File>();
	private static Vector<File> RMSE_FILE = new Vector<File>();
	private static Vector<File> CURRENT_ITERATION_DIRECTORY = new Vector<File>();
	private static Vector<File> CODEBOOK_USER_DIR= new Vector<File>();
	private static Vector<File> CODEBOOK_ITEM_DIR= new Vector<File>();
	private static Vector<String> CODEBOOK_ITEM_DIR_PATH = new Vector<String>();
	private static Vector<String> CODEBOOK_USER_DIR_PATH = new Vector<String>();
	private static Vector<String> RMSE_DIR_PATH = new Vector<String>();
	
	
	//names of files and dirs
		public static String CODEBOOK_USER_DIR_NAME = "CodeBookUser";
		public static String CODEBOOK_ITEM_DIR_NAME = "CodeBookItem";
		public static String RMSE_DIR_NAME = "RMSE";
		public static String PROFILERS_DIR_NAME = "Profilers";
		public static String MAPREDUCE_FINAL_FILE_NAME_PREFIX = "part-";
	
	public static File getCodeBookUserFile(){
		return CODEBOOK_USER_FILE.lastElement();
	}
	
	public static void addCodeBookUserFile(File f){
		CODEBOOK_USER_FILE.add(f);
	}
	
	public static File getCodeBookItemFile(){
		return CODEBOOK_ITEM_FILE.lastElement();
	}
	
	public static void addCodeBookItemFile(File f){
		CODEBOOK_ITEM_FILE.add(f);
	}
	
	public static File getRMSEFile(){
		return RMSE_FILE.lastElement();
	}
	
	public static void addRMSEFile(File f){
		 RMSE_FILE.add(f);
	}
	
	public static File getCurrentIterationDir(){
		return CURRENT_ITERATION_DIRECTORY.lastElement();
	}
	
	public static void addCurrentIterationDir(File f){
		CURRENT_ITERATION_DIRECTORY.add(f);
	}
	
	public static File getCodeBookUserDir(){
		return CODEBOOK_USER_DIR.lastElement();
	}
	
	public static void addCodeBookUserDir(File f){
		CODEBOOK_USER_DIR.add(f);
	}
	
	public static File getCodeBookItemDir(){
		return CODEBOOK_ITEM_DIR.lastElement();
	}
	
	public static void addCodeBookItemDir(File f){
		CODEBOOK_ITEM_DIR.add(f);
	}
	
	public static String getCodeBookItemDirPath(){
		return CODEBOOK_ITEM_DIR_PATH.lastElement();
	}
	
	
	public static void addCodeBookItemDirPath(String p){
		CODEBOOK_ITEM_DIR_PATH.add(p);
	}
	
	public static String getCodeBookUserDirPath(){
		return CODEBOOK_USER_DIR_PATH.lastElement();
	}
	
	public static void addCodeBookUserDirPath(String p){
		CODEBOOK_USER_DIR_PATH.add(p);
	}
	
	public static String getRMSEDirPath(){
		return RMSE_DIR_PATH.lastElement();
	}
	
	public static void addRMSEDirPath(String s){
		RMSE_DIR_PATH.add(s);
	}

	public static void createNewPathsForIteration(int iteration) {
		String directoryName = "foo" + iteration;
		File dir = new File(directoryName);
		if (dir.exists()){
			try {
				FileUtils.deleteDirectory(dir);
			} catch (IOException e) {
				System.out.println("Problem with deletion");
				System.exit(-1);
			}
		}
		addCodeBookItemDirPath(directoryName + "/"  + CODEBOOK_ITEM_DIR_NAME);
		addCodeBookUserDirPath(directoryName + "/"  + CODEBOOK_USER_DIR_NAME);
	}
	
	public static void updateCodeBookItems(int iteration){
		String itemDir = "foo" + iteration + "/" + CODEBOOK_ITEM_DIR_NAME;
		File f = findInDir(itemDir , iteration);
		CODEBOOK_ITEM_FILE.add(f);
	}
	
	public static void updateCodeBookUser(int iteration){
		String itemDir = "foo" + iteration + "/" + CODEBOOK_USER_DIR_NAME;
		File f = findInDir(itemDir , iteration);
		CODEBOOK_USER_FILE.add(f);
	}
	

	private static File findInDir(String dir , int iteration) {
		File file = new File(dir);
		File[] listFiles = file.listFiles();
		try{
		for(File f : listFiles){
			if (f.getName().startsWith(MAPREDUCE_FINAL_FILE_NAME_PREFIX)){
				return f;
			}
		}
		}catch(Exception e){
			e.printStackTrace();
			System.out.println("Cant find the file:"  + dir + " Iteration:" + iteration);
			System.exit(-1);
		}
		return null;
		
	}
	
	

}
